var app = getApp()
Page({


  data: {
    isPoint: false,
    item: [],
    sum: 0,
    isFine: true
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '面试名单'
    })
    var i
    var a = 0
    var items = app.globalData.data
    var item = []
    for (i = 0; i < app.globalData.sum; i++) {
      //判断是否被第一志愿淘汰
        if (items[i].isPass1 == 1) {
          //判断是否发送面试短信
          if (items[i].isUsed2 == 0 && items[i].isUsed1 == 1 && items[i].isUsed == 0) {
            item.push(items[i])
            a++
          }
          //判断是否被第二志愿淘汰
        }
        if (items[i].isPass1 == 0) {
          if (items[i].isPass2 == 1) {
            //判断是否发送通知短信
            if (items[i].isUsed2 == 0 && items[i].isUsed1 == 1 && items[i].isUsed == 0) {
              item.push(items[i])
              a++
            }
          }
        }
    }
    app.globalData.interview = item
    this.setData({
      item: item,
      sum: a
    })
  },

  ListTouchStart(e) {
    console.log(e, e.touches[0])
    var aaa = e.currentTarget.dataset.postid
    // this.data.jobID.indexOf("y") == 0
    // console.log(aaa.substr(aaa.length - 1, 1))
    this.setData({
      ListTouchStartx: e.touches[0].pageX,
      ListTouchStarty: e.touches[0].pagey,
      userIndex: aaa
    })
  },


  // ListTouch计算滚动
  ListTouchEnd(e) {
    var that = this
    var aaa = e.currentTarget.dataset.postid
    console.log(e, e.changedTouches[0].pageX)
    this.setData({
      ListTouchEndx: e.changedTouches[0].pageX,
      ListTouchEndy: e.changedTouches[0].pagey,
    })
    console.log(this.data.userIndex, aaa)
    if ((this.data.ListTouchStartx - this.data.ListTouchEndx) > 5 && this.data.userIndex == aaa) {
      app.globalData.interview[aaa].userLeft = 1
      that.setData({
        item: app.globalData.interview,
        isFine: false
      })
    } else if ((this.data.ListTouchStartx - this.data.ListTouchEndx) < -5 && this.data.userIndex == aaa) {
      app.globalData.interview[aaa].userLeft = 0
      that.setData({
        item: app.globalData.interview,
        isFine: true
      })
    }
  },

  deleteTap: function (event) {
    var that = this
    var i, j = that.data.userIndex
    //是否第一志愿
    if (that.data.item[j].isPass1 == 1) {
      for (i = 0; i < app.globalData.sum; i++) {
        if (app.globalData.data[i].id == that.data.item[j].id) {
          app.globalData.data[i].isPass1 = 0
          app.globalData.data[i].isPoint1 = 0
          app.globalData.data[i].userLeft = 0
          app.globalData.data[i].isOut = 1
        }
      }
      // 更新知晓云数据项  分数 ，备注 ， 是否通过第一志愿
      let tableName = 'data'
      let recordID = this.data.item[that.data.userIndex].id // 数据行 id
      
      let Product = new wx.BaaS.TableObject(tableName)
      let product = Product.getWithoutData(recordID)
      product.set('isPass1', parseFloat(0))
      product.set('isOut', parseFloat(1))
      product.update().then(res => {
        console.log(res)
      }, err => {
        // err
      })
    }
    //若为第二次面试
    if (that.data.item[j].isPass1 == 0) {
      var i, j = that.data.userIndex
      for (i = 0; i < app.globalData.sum; i++) {
        if (app.globalData.data[i].id == that.data.item[j].id) {
          app.globalData.data[i].isPass2 = 0
          app.globalData.data[i].isPoint1 = 0
          app.globalData.data[i].userLeft = 0
        }
      }
    }
    this.onLoad()
     // 更新知晓云数据项  分数 ，备注 ， 是否通过第一志愿
      let tableName = 'data'
      let recordID = this.data.item[that.data.userIndex].id// 数据行 id
      
      let Product = new wx.BaaS.TableObject(tableName)
      let product = Product.getWithoutData(recordID)
      product.set('isPass2', parseFloat(0))
      product.update().then(res => {
        console.log(res)
      }, err => {
        // err
      })
     
  },

  set_defaultTap: function (e) {
    var that = this
    var i, j = that.data.userIndex
    for (i = 0; i < app.globalData.sum; i++) {
      if (app.globalData.data[i].id == that.data.item[j].id) {
        app.globalData.data[i].isUsed1 = 0
        app.globalData.data[i].isPoint1 = 0
        app.globalData.data[i].userLeft = 0
      }
    }
    this.onLoad()
    //更新知晓云数据
    let tableName = 'data'
      let recordID = this.data.item[that.data.userIndex].id // 数据行 id
      
      let Product = new wx.BaaS.TableObject(tableName)
      let product = Product.getWithoutData(recordID)
      product.set('isUsed1', parseFloat(0))
      product.update().then(res => {
        console.log(res)
      }, err => {
        // err
      })
  },

  OninterviewingTap: function (e) {
    console.log(e)
    var postId = e.currentTarget.dataset.postid
    wx.redirectTo({
      url: '/pages/mark_1/mark_1?postid=' + postId,
    })
   
  }

})